package com.shaozi.nc.configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;

import com.shaozi.lib.execpool.RunnableExecutor;
import com.shaozi.lib.observer.CommonObservable;
import com.shaozi.nc.manage.OrgPushMsgManage;
import com.shaozi.nc.manage.PushMsgManage;
import com.shaozi.nc.manage.observer.AllPushMsgObserver;
import com.shaozi.nc.manage.observer.ApproveObserver;
import com.shaozi.nc.manage.observer.AttendanceObserver;
import com.shaozi.nc.manage.observer.MQConsumerObserver;
import com.shaozi.nc.manage.observer.ReportObserver;
import com.shaozi.nc.manage.observer.SecretaryApiStartupObserver;
import com.shaozi.nc.manage.observer.TaskObserver;
import com.shaozi.nc.manage.secretary.AttendanceManage;
import com.shaozi.nc.manage.secretary.SecretaryApiQuartzManage;
import com.shaozi.nc.manage.secretary.SecretaryManage;
import com.shaozi.nc.rpc.UserInfoRPC;
import com.shaozi.utils.common.BeanUtils;
import com.shaozi.utils.common.ClassPathProperties;

@Configuration
@Order(2)
public class BeanConfiguration {

	@Autowired
	private RunnableExecutor runnableExecutor;

	public BeanConfiguration() {
		super();
	}

	@Bean(initMethod = "load", name = "confPropertiesBean")
	public ClassPathProperties classPathProperties() {
		ClassPathProperties cp = new ClassPathProperties();
		cp.setPropertiesFile("/conf.properties");
		return cp;
	}

	/******************** 观察者模式 *************************/

	@Bean
	public AllPushMsgObserver allPushMsgObserver() {
		return new AllPushMsgObserver();
	}

	@Bean
	public MQConsumerObserver mqConsumerObserver() {
		return new MQConsumerObserver();
	}

	@Bean
	public SecretaryApiStartupObserver secretaryApiStartupObserver() {
		return new SecretaryApiStartupObserver();
	}

	// @Bean(name = "companyObservable", initMethod = "trans")
	@Bean(name = "companyObservable")
	public CommonObservable companyObservable(AllPushMsgObserver allPushMsgObserver,
			MQConsumerObserver mqConsumerObserver, SecretaryApiStartupObserver secretaryApiStartupObserver) {
		CommonObservable companyObservable = new CommonObservable();
		companyObservable.addObserver(allPushMsgObserver);
		companyObservable.addObserver(mqConsumerObserver);
		companyObservable.addObserver(secretaryApiStartupObserver);
		System.out.println("companyObservable:" + allPushMsgObserver);
		return companyObservable;
	}

	/******************** 观察者模式end *************************/

	@Bean("pushMsgMgr")
	public PushMsgManage pushMsgMgr() {
		return new PushMsgManage();
	}

	@Bean("orgPushMsgMgr")
	public OrgPushMsgManage orgPushMsgMgr() {
		return new OrgPushMsgManage();
	}

	@Bean("secretaryMgr")
	public SecretaryManage secretaryMgr() {
		return new SecretaryManage();
	}

	@Bean("userRequest")
	public UserInfoRPC userRequest() {
		return new UserInfoRPC();
	}

	@Bean
	public AttendanceManage attendanceManage() {
		return new AttendanceManage();
	}

	@Bean("secretaryApiQuartzMgr")
	public SecretaryApiQuartzManage secretaryApiQuartzMgr() {
		return new SecretaryApiQuartzManage();
	}

	@Bean
	public ReportObserver reportObserver() {
		return new ReportObserver();
	}

	@Bean
	public AttendanceObserver attendanceObserver() {
		return new AttendanceObserver();
	}

	@Bean
	public ApproveObserver approveObserver() {
		return new ApproveObserver();
	}

	@Bean
	public TaskObserver taskObserver() {
		return new TaskObserver();
	}

	@PostConstruct
	public void init() {
		// RunnableExecutor runnableExecutor = (RunnableExecutor)
		// BeanUtils.getBean("runnableExecutor");
		runnableExecutor.startService();
	}

	@PreDestroy
	public void deinit() {
		// RunnableExecutor runnableExecutor = (RunnableExecutor)
		// BeanUtils.getBean("runnableExecutor");
		runnableExecutor.shutdown();
	}
}
